package com.example.composestudy.ui.widget

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composestudy.ui.theme.MyIcons

@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun MyTopAppBar(
    title: String,
    titleFontSize: TextUnit = 16.sp,
    titleFontWeight: FontWeight = FontWeight.W700,
    titleFontColor: Color = MaterialTheme.colorScheme.primary,
    appBarBgColor: Color = MaterialTheme.colorScheme.primaryContainer,
    showBackIcon: Boolean = true,
    actions: @Composable RowScope.() -> Unit = {},
    navigationIconClick: (() -> Unit)? = null,
) {
    Box(Modifier.height(60.dp), contentAlignment = Alignment.Center) {
        TopAppBar(
            modifier = Modifier.fillMaxWidth(),
            title = {
                Text(
                    title,
                    fontSize = titleFontSize,
                    fontWeight = titleFontWeight,
                    color = titleFontColor,
                    textAlign = TextAlign.Center,
                    modifier = Modifier.fillMaxWidth()
                )
            },
            actions = {
                IconFont(
                    MyIcons.toolbarBack,
                    color = Color.Transparent,
                    modifier = Modifier.padding(8.dp),
                    size = 20.dp
                )
            },
            navigationIcon = {
                if (showBackIcon) {
                    IconFont(MyIcons.toolbarBack,
                        Modifier
                            .clickable {
                                navigationIconClick?.invoke()
                            }
                            .padding(8.dp), size = 20.dp)
                }
            },
            colors = TopAppBarDefaults.topAppBarColors(containerColor = appBarBgColor),
        )
    }
}

@Preview
@Composable
fun PreviewMyTopAppBar() {
    MyTopAppBar("title")
}